package com.app.game.control;

import java.util.ArrayList;
import java.util.HashMap;

import com.app.ThreadManager;
import com.app.client.ServerHandler;
import com.app.factories.BetFactory;
import com.app.game.bet.Bet;
import com.app.game.chat.Message;
import com.app.game.other.Credit;
import com.app.game.other.Player;
import com.app.observers.ServerObserver;

public class HumanPlayer implements Player, ServerObserver {

    /**
     *
     */
    private static final long serialVersionUID = 4214608937481055675L;
    private transient ServerHandler handler;
    private transient ArrayList<Bet> bets = new ArrayList<Bet>();
    private Credit credits = new Credit();
    private String name;
    private Integer id;

    public HumanPlayer(String name) {
        this.name = name;
    }

    @Override
    public Bet makeBet(BetFactory factory, int type, Credit credit) {
        this.credits.substract(credit);
        Bet bet = factory.createBet(type, credit);
        bets.add(bet);
        return bet;
    }

    @Override
    public void cancelBet(Bet bet) {
        bets.remove(bet);
    }

    @Override
    public void finishRound() {
        handler.sendRoundDone();
    }

    @Override
    public void ready() {
        handler.sendReady();

    }

    @Override
    public void sendMessage(Message msg) {
        handler.sendMessage(msg);

    }

    @Override
    public void setServerHandler(ServerHandler handler) {
        this.handler = handler;
        
    }

    @Override
    public ServerHandler getServerHandler() {
        return handler;
    }

    @Override
    public void updatePlayers(HashMap<Integer, Player> playerlist) {
        
    }

    @Override
    public void updatePlayer(Player player) {
        credits = player.getCredits();
        id = player.getId();

    }

    @Override
    public void updateResult(Integer result) {
        for (Bet bet : bets) {
            Credit win = bet.checkResults(result);
            credits.add(win);
        }

    }

    @Override
    public void updateReadyNotification(Integer playerId) {
        // TODO Auto-generated method stub
    }

    @Override
    public void updateMessage(Message msg) {
        // TODO Auto-generated method stub
    }

    @Override
    public void updateGameStarting() {
        // TODO Auto-generated method stub
    }

    @Override
    public void updateGameOver() {
        // TODO Auto-generated method stub
    }

    @Override
    public Credit getCredits() {
        return credits;
    }

    @Override
    public void setCredits(Credit credits) {
        this.credits = credits;
    }

    @Override
    public Integer getId() {
        return id;
    }

    @Override
    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }
}
